package com.mohe.zhihui.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.mohe.zhihui.dao.IDao;
import com.mohe.zhihui.model.Admin;
import com.mohe.zhihui.service.interfaces.IAdmin;

public class AdminImpl implements IAdmin {

	private IDao<Admin> adminDao;

	public IDao<Admin> getAdminDao() {
		return adminDao;
	}

	public void setAdminDao(IDao<Admin> adminDao) {
		this.adminDao = adminDao;
	}

	@Override
	public Admin getAdminByPwd(String username, String password) {

		StringBuffer hql = new StringBuffer();
		List<Object> valueS = new ArrayList<Object>();
		hql.append("where name = ? and password = ?");
		valueS.add(username);
		valueS.add(password);

		List<Admin> list = adminDao.query(hql.toString(), valueS);

		if (null != list && !list.isEmpty()) {
			return list.get(0);
		}

		return null;
	}

	@Override
	public boolean hasAdmin() {

		List<Object> valueS = new ArrayList<Object>();
		StringBuffer where = new StringBuffer();

		where.append("where adminName = ?");
		valueS.add("admin");
		List<Admin> list = adminDao.query(where.toString(), valueS);
		if (list != null && list.size() > 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean insert(Admin admin) {
		return adminDao.insert(admin);
	}

	@Override
	public boolean deleteAdminList(List<Admin> list) {
		return adminDao.delete(list);
	}

	@Override
	public List<Admin> getAdminList(Admin admin, Integer currPage,
			Integer pageSize) {
		StringBuffer where = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		where.append("from Admin where 1=1");
		if(admin!=null && admin.getName()!=null && !admin.getName().equals("")){
			where.append(" and name like ?");
			params.add("%%"+admin.getName()+"%%");
		}
//		if(admin!=null && admin.getRole()!=null && admin.getRole().getTimeKey()!=null && !admin.getRole().getTimeKey().equals("")){
//			where.append(" and role.timeKey = ?");
//			params.add(admin.getRole().getTimeKey());
//		}
		List<Admin> list = adminDao.query(where.toString(), (currPage-1)*pageSize, pageSize, params);
		if(list!=null && list.size()>0){
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(Admin admin) {
		StringBuffer where = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		where.append("from Admin where 1=1");
		if(admin!=null && admin.getName()!=null && !admin.getName().equals("")){
			where.append(" and name like ?");
			params.add("%%"+admin.getName()+"%%");
		}
//		if(admin!=null && admin.getRole()!=null && admin.getRole().getTimeKey()!=null && !admin.getRole().getTimeKey().equals("")){
//			where.append(" and role.timeKey = ?");
//			params.add(admin.getRole().getTimeKey());
//		}
		return adminDao.getCount(where.toString(), params);
	}

	@Override
	public Admin getAdminById(Long timeKey) {
		Admin admin = adminDao.getByKey(timeKey);
		if(admin!=null){
			return admin;
		}
		return null;
	}

	@Override
	public Admin getAdminByName(String name) {
		StringBuffer where = new StringBuffer();
		where.append("from Admin where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(name!=null && !name.equals("")){
			where.append(" and adminName = ?");
			params.add(name);
		}
		List<Admin> list = adminDao.query(where.toString(), params);
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public boolean updateAdmin(Admin admin) {
		return adminDao.update(admin);
	}

	@Override
	public boolean deleteAdmin(Long timeKey) {
		return adminDao.delete(timeKey);
	}

}
